(function(){
	'use strict';
	angular.module('controllers.workOrderData.eventMgmt')
	.controller('EventStatisticController',EventStatisticController);
	/**
	 * 衡量指标
	 * h2001：事件总数
	 * h2002：已关闭事件
	 * h2003: 规定时间内解决事件
	 * h2004: 平均解决时间
	 * h2005: 平均审核时间
	 * h2006: 平均关闭时间
	 * h2007: 升级事件所占比率
	 * h2008: 重开事件所占比率
	 */
	function EventStatisticController($scope,workOrderDataService,CoreLoading,PublicFn,$timeout,eventStatisticFactory,COREDATA){
		$scope.initQuery = function(){
			workOrderDataService.queryMeasureIndex({code:'2'},function(res){
				$scope.measureIndexs = res.data;
				$scope.search();
			});
		};

		$scope.flag = {
			state: true,
			show: 'view',
			arrow: false,
			showPie: false
		};
		$scope.sendData = workOrderDataService.getSendData('1');
		$scope.sendData.measureIndexCode = 'h2001';
		//导出路径
		$scope.formPrefix = COREDATA.FORMPREFIX+'workOrderData/eventMgmt/export';

		var myEcharts = echarts.init(document.getElementById('doc'));
		$scope.search = function(){
			$scope.flag.state = !$scope.flag.state;
			var xAxisData = [];
			var seriesData = [];
			var option = {};
			var code,obj;
			if(!$scope.flag.state){
				obj = workOrderDataService.fmtSendData($scope.sendData);
				code = obj.measureIndexCode;
				var timeTitle = PublicFn.fmtDateTitle(obj);
				$scope.name = workOrderDataService.getName(code,$scope.measureIndexs,'measureIndexCode','measureIndexName');
				CoreLoading.setState(true);
				eventStatisticFactory.query(obj,function(res){
					if(res.state === '10000' && res.data.length >=1){
						myEcharts.clear();
						$scope.send = obj;
						if(code === 'h2001'){
							$scope.name = '事件单数量统计';
						}else if(code === 'h2002' || code === 'h2003'){
							$scope.name += '数量统计'
						}
						var smallName = eventStatisticFactory.getShortName($scope.send);
						$scope.send.title = timeTitle + smallName + $scope.name;
						//汇总
						$scope.totalNum = 0;
						angular.forEach(res.data,function(item){
							xAxisData.push(item.filedName);
							if(code === 'h2007' || code === 'h2008'){
								seriesData.push(Number(item.percentage.replace('%','')).toFixed(2));
							}else{
								seriesData.push(item.filedNum);
								$scope.totalNum += Number(item.filedNum.replace('%',''));
							}
						});
						$scope.items = res.data;
						//当衡量指标为升级事件所占比率、重开事件所占比率时
						if(code === 'h2007' || code === 'h2008'){
							$scope.showPrcentage = true;
						}else {
							$scope.showPrcentage = false;
						}
						option = workOrderDataService.getEchartsData(xAxisData,seriesData,$scope.send.title,$scope.name);
						option.legend.y = '30';
						eventStatisticFactory.fmtOption(code,option);
						//计算echarts的legend和图之间的位置
						// option.grid = {
						// 	x2:$scope.name.length * 12 + 50
						// };
						myEcharts.setOption(option);
						$scope.outData = workOrderDataService.exportView(myEcharts,$scope.send);
						$scope.flag.arrow = true;
            $scope.flag.show = 'view';
					}else if(res.data.length === 0){
						workOrderDataService.queryNoDataOrError($scope.flag,'查询无数据');
					}else {
						workOrderDataService.queryNoDataOrError($scope.flag,'查询错误');
					}
					CoreLoading.setState(false);
				});
			}
		};

		//监控页面时间，如果时间出错，则查询按钮禁止
		$scope.$watch('myForm.$invalid', function(newValue) {
    	newValue ? $scope.flag.arrow = false : $scope.flag.arrow = true;
		});
	}
	EventStatisticController.$inject = ['$scope','workOrderDataService','CoreLoading','PublicFn','$timeout','eventStatisticFactory','COREDATA'];
})();